package www.qiyue.pt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.qiyue.utils.UUIDGenerator;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class ais_contract {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("test")
    public void contract(){
        String delete="DELETE FROM ais_contract_warnings";
        jdbcTemplate.update(delete);
        String contract_fee="SELECT MAX(end_date) as end_date,contract_code FROM ais_contract_fee GROUP BY contract_code";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(contract_fee);
        String contract="SELECT * FROM ais_contract WHERE start_date is not NULL and status_id='02'";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(contract);
        String time="select * FROM ais_time";
        List<Map<String, Object>> times = jdbcTemplate.queryForList(time);
        if (maps!=null&&maps.size()>0){
            for (Map<String, Object> map1 : maps) {

                String contract_code1 = map1.get("contract_code").toString();

                String start_date = map1.get("start_date").toString();

                int i=0;
                String endDate=null;
                String code=null;
                String end_date=null;
                if (mapList!=null&&mapList.size()>0){
                    for (Map<String, Object> map : mapList) {
                        String contract_code = map.get("contract_code").toString();
                        if (contract_code1.equals(contract_code)){
                            end_date=map.get("end_date").toString();
                            endDate = time(end_date);
                            code = getCode(times, endDate);


                        }else {
                            i++;
                        }
                        if (i==mapList.size()){
                            endDate = time(start_date);
                            code = getCode(times, endDate);
                        }

                    }
                }else {
                    endDate=time(start_date);
                    code = getCode(times, endDate);
                }
                if (code!=null) {
                    update(map1, code,end_date);
                }
            }

        }
    }
    private String getCode(List<Map<String, Object>> table, String parameter) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        result = table;
        // 转换String类型到bigDecimal
        BigDecimal pmt = new BigDecimal(parameter);
        // 判断查询数据不为空
        if (result != null && result.size() > 0) {
            for (Map<String, Object> entity : result) {
                // 取最小值
                BigDecimal min = new BigDecimal(entity.get("min") == null ? "0"
                        : entity.get("min").toString());
                // 取最大值
                BigDecimal max = new BigDecimal(entity.get("max") == null ? "0"
                        : entity.get("max").toString());
                // 返回数据字段
                String code = null;
                if (!"00".equals(entity.get("code").toString())) {
                    // 编号
                    code = entity.get("code").toString();
                    // 判断只存在最大值时
                    if (min == null || min.compareTo(new BigDecimal(0)) == 0) {
                        if (pmt.compareTo(max) < 0) {
                            return code;
                        }
                    }
                    // 判断只存在最大值时
                    if (max == null || max.compareTo(new BigDecimal(0)) == 0) {
                        if (pmt.compareTo(min) > 0 || pmt.compareTo(min) == 0) {
                            return code;
                        }
                    }
                    // 即存在最大值也才存在最小值
                    if ((pmt.compareTo(min) > 0 || pmt.compareTo(min) == 0)
                            && pmt.compareTo(max) < 0) {
                        return code;
                    }
                }
            }
        }
        return null;

    }
    private String time(String str1){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String str2 = sdf.format(date);
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        try {
            bef.setTime(sdf.parse(str1));
            aft.setTime(sdf.parse(str2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return (Math.abs(month + result))+"";
    }
    private void update(Map<String,Object> map,String code,String end){
        String id = map.get("id")==null?"":map.get("id").toString();
        String contract_code = map.get("contract_code")==null?"":map.get("contract_code").toString();
        String house_id = map.get("house_id")==null?"":map.get("house_id").toString();
        String house_code = map.get("house_code")==null?"":map.get("house_code").toString();
        String customer_id = map.get("customer_id")==null?"":map.get("customer_id").toString();
        String customer_name = map.get("customer_name")==null?"":map.get("customer_name").toString();
        String company_id = map.get("company_id")==null?"":map.get("company_id").toString();
        Map<String, Object> temp = new HashMap<String, Object>();
        temp.put("id", UUIDGenerator.generate());
        temp.put("contract_id",id);
        temp.put("contract_code",contract_code);
        temp.put("house_id",house_id);
        temp.put("house_code",house_code);
        temp.put("customer_id",customer_id);
        temp.put("customer_name",customer_name);
        temp.put("company_id",company_id);
        temp.put("warning_date",code);
        temp.put("fee_date",end);

        System.out.println(temp);
    }
    public  void  test0666(){
        String sql ="SELECT * FROM ais_house";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : mapList) {

        }
    }
}
